package com.example.demo.proxy;

import com.example.demo.annotation.PreDataProxy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.function.Consumer;

@Component
public class DataProxyInvoke implements ApplicationContextAware {

    public DataProxy invoke(Class<?> clazz) {
        PreDataProxy proxy = clazz.getAnnotation(PreDataProxy.class);
        if (proxy == null) {
            return null;
        }
        return getBean(proxy.value());
    }

    private <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
